﻿using Mes.Pendant.Workstation.Dto;
using Mes.Pendant.Workstation.IAppService;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Threading.Tasks;

namespace Mes.Pendant.Web.Controllers
{
	public class WorkstationController : Controller
	{
		private readonly IWorkstationAppService _workstationAppService;

		public WorkstationController(IWorkstationAppService workstationAppService)
		{
			_workstationAppService = workstationAppService;
		}


		public IActionResult WorkstationList()
		{
			return View();
		}

		public IActionResult AddWorkstationView()
		{
			return View();
		}

		public IActionResult UpdateWorkstationView(int id)
		{
			ViewBag.id = id;
			return View();
		}

		/// <summary>
		/// 工作站列表、分页、查询
		/// </summary>
		/// <param name="workstationName">工作站名称</param>
		/// <param name="page">当前页</param>
		/// <param name="limit">每页条数</param>
		/// <returns>工作站列表数据</returns>
		[HttpGet]
		public async Task<JsonResult> GetWorkstationList(string? workstationName, int page = 1, int limit = 10)
		{
			var result = await _workstationAppService.GetWorkstationList(workstationName, page, limit);
			//return  Json(result);
			return Json(new { data = result.Queryable, count = result.RowCount, code = 0, msg = "" });
		}

		/// <summary>
		/// 新增工作站
		/// </summary>
		/// <param name="addWorkstationDto">新增工作站实体</param>
		/// <returns>实体自增主键</returns>
		[HttpPost]
		public async Task<int> AddWorkstation(AddWorkstationDto addDto)
		{
			int rowNum = await _workstationAppService.AddWorkstation(addDto);
			return rowNum;
		}

		/// <summary>
		/// 删除工作站
		/// </summary>
		/// <param name="id">实体主键</param>
		/// <returns>受影响的行数</returns>
		[HttpPost]
		public async Task<int> RemoveWorkstationById(int id)
		{
			int rowNum = await _workstationAppService.RemoveWorkstationById(id);
			return rowNum;
		}


		/// <summary>
		/// 查看工作站详情
		/// </summary>
		/// <param name="id">工作站主键</param>
		/// <returns>工作站实体</returns>
		[HttpGet]
		public async Task<EditWorkstationDto> FindWorkstationById(int id)
		{
			var workstationEntity = await _workstationAppService.FindWorkstationById(id);
			return workstationEntity;
		}

		/// <summary>
		/// 更新工作站
		/// </summary>
		/// <param name="editWorkstationDto">更新工作站实体</param>
		/// <returns>受影响的行数</returns>
		[HttpPost]
		public async Task<int> EditWorkstation(EditWorkstationDto editWorkstationDto)
		{
			int rowNum = await _workstationAppService.UpdateWorkstation(editWorkstationDto);
			return rowNum;
		}

		[HttpGet]
		public async Task<JsonResult> GetWorkstationListRedis()
		{
			var list = await _workstationAppService.GetWorkstationListRedis();
			return Json(new { data = list, code = 0 });
		}


	}
}
